<!--
 * @Description:退款管理
-->
<template>
  <div class="registered-container refund appMainWarp">
    <el-form :inline="true" :model="searchKey" class="demo-form-inline">
      <!-- <el-form-item>
        <el-button :type="0 == quaflat ? 'primary':''" @click="getlisttable(0)">昨日</el-button>
        <el-button :type="1 == quaflat ? 'primary':''" @click="getlisttable(1)">近7天</el-button>
        <el-button :type="2 == quaflat ? 'primary':''" @click="getlisttable(2)">近一个月</el-button>
      </el-form-item> -->

      <el-form-item label="交易时间:">
        <el-date-picker
          v-model="searchKey.time"
          type="date"
          placeholder="选择日期"
          value-format="yyyy-MM-dd"
          :picker-options="pickerOptions"
        />
        <!-- <el-date-picker
          v-model="searchKey.time"
          type="daterange"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          value-format="yyyy-MM-dd"
          :picker-options="pickerOptions"
        /> -->
      </el-form-item>
      <el-form-item label="选择支付场景:">
        <el-select v-model="searchKey.system_no" placeholder="请选择支付场景" clearable>
          <el-option
            v-for="item in searchKey.System"
            :key="item.pay_scene_no"
            :label="item.pay_scene_name"
            :value="item.pay_scene_no"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="支付场景订单号:">
        <el-input v-model="searchKey.out_trade_no" placeholder="请输入支付场景订单号" @change="suborder_no" />
      </el-form-item>
      <!-- <el-form-item label="平台订单号:">
        <el-input v-model="searchKey.order_no" placeholder="请输入订单号" @change="suborder_no" />
      </el-form-item> -->

      <el-form-item label="支付方式:">
        <el-select v-model="searchKey.pay_style" placeholder="请选择">
          <el-option
            v-for="item in searchKey.PayStyle"
            :key="item.pay_no"
            :label="item.name"
            :value="item.pay_no"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="支付金额:">
        <el-input v-model="searchKey.payfee" placeholder="请输入支付金额" />
      </el-form-item>
      <el-form-item label="患者id:">
        <el-input v-model="searchKey.patientid" placeholder="请输入患者id" @change="suborder_no" />
      </el-form-item>

      <el-form-item label="患者姓名:">
        <el-input v-model="searchKey.patientiname" placeholder="请输入患者姓名" @change="suborder_no" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" @click="onLoad">查询</el-button>
      </el-form-item>
      <el-form-item>
        <el-button type="success" icon="el-icon-download" @click="onExport">导出明细</el-button>
      </el-form-item>
    </el-form>

    <template>
      <el-table
        ref="table"
        v-loading="page.tableLoading"
        v-el-height-adaptive-table="{bottomOffset: 90}"
        max-height="500px"
        center
        class="table"
        :data="searchData"
        stripe
        border
      >
        <el-table-column type="index" width="90" label="序号" />
        <el-table-column label="患者ID" align="center">
          <template slot-scope="scope">{{ scope.row.patient_id||'-' }}</template>
        </el-table-column>
        <el-table-column label="支付场景" align="center">
          <template slot-scope="scope">{{ scope.row.pay_scene_name||'-' }}</template>
        </el-table-column>
        <el-table-column label="支付场景订单号" align="center">
          <template slot-scope="scope">{{ scope.row.self_order_no||'-' }}</template>
        </el-table-column>
        <el-table-column label="交易流水号" align="center">
          <template slot-scope="scope">{{ scope.row.three_order_no ||'-' }}</template>
        </el-table-column>
        <el-table-column label="业务类型" align="center">
          <template slot-scope="scope">{{ scope.row.bill_type ||'-' }}</template>
        </el-table-column>
        <el-table-column label="交易金额" align="center">
          <template slot-scope="scope">{{ scope.row.pay_fee ||'-' }}</template>
        </el-table-column>
        <el-table-column label="支付方式" align="center">
          <template slot-scope="scope">{{ scope.row.pay_type_name }}</template>
        </el-table-column>
        <el-table-column label="交易状态" align="center">
          <template slot-scope="scope">{{ scope.row.desc ||'-' }}</template>
        </el-table-column>
        <el-table-column label="交易时间" align="center">
          <template slot-scope="scope">{{ scope.row.trans_time ||'-' }}</template>
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button v-if="scope.row.refund_flag === 1" size="mini" :underline="false" type="primary" @click="refundBtn(scope.row)">&nbsp;&nbsp;&nbsp;退款&nbsp;&nbsp;&nbsp;</el-button>
            <el-link v-if="scope.row.refund_flag === 2" size="mini" :underline="false" type="info" disabled>{{ scope.row.msg }}</el-link>
          </template>
        </el-table-column>
        <!-- <el-table-column label="操作">
          <template slot-scope="props">
            <el-link v-if="props.row.flag == 1" :underline="false" />
            <el-link v-if="props.row.flag == 2" size="mini" :underline="false" type="primary" @click="refundBtn(props.row)">&nbsp;&nbsp;&nbsp;退款&nbsp;&nbsp;&nbsp;</el-link>
            <el-tooltip class="item" effect="dark" content="订单过期，不可退款" placement="top-start">
              <el-link v-if="props.row.flag == 3" size="mini" :underline="false" type="info">&nbsp;&nbsp;&nbsp;退款&nbsp;&nbsp;&nbsp;</el-link>
            </el-tooltip>
            <el-link v-if="props.row.flag == 4" type="primary" :underline="false">&nbsp;&nbsp;&nbsp;退款中&nbsp;&nbsp;&nbsp;</el-link>
            <el-link size="mini" type="success" :underline="false" @click="expandChange(props.row)">{{ props.row.rowFlag?'收起详情':'查看详情' }}</el-link>
          </template>
        </el-table-column>
        <el-table-column type="expand" width="1">
          <template slot-scope="props">
            <el-tabs type="border-card">
              <el-tab-pane label="退款详情">
                <div>
                  <el-form label-width="100px" label-position="left">
                    <el-form-item label="退款信息:" label-width="100px">
                      <div v-for="(item,index) in props.row.info" :key="index" class="details">
                        <div class="orderItem">
                          <span>退款金额:</span>
                          {{ item.refund_fee||'-' }}
                        </div>
                        <div class="orderItem">
                          <span>退款状态:</span>
                          {{ item.refund_status_state||'-' }}
                        </div>
                        <div class="orderItem">
                          <span>退款成功时间:</span>
                          {{ item.refund_success_time }}
                        </div>
                        <div class="orderItem">
                          <span>退款账户:</span>
                          {{ item.refund_recv_accout }}
                        </div>
                      </div>
                      <div v-if="props.row.info.length <=0" class="details">暂无数据</div>

                    </el-form-item>
                  </el-form>
                </div>
              </el-tab-pane>
            </el-tabs>
          </template>
        </el-table-column> -->
      </el-table>
    </template>

    <el-dialog title="退款" :visible.sync="addModel.flag" width="500px">
      <span class="tip-title-w">进行退款操作前建议先与HIS系统账单进行核对，以免造成异常账单！</span>
      <el-form ref="addModel" :model="addModel.item" label-width="60px">
        <el-form-item label="备注:">
          <el-input v-model="des" type="textarea" placeholder="请输入备注" autocomplete="off" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="addModel.flag = false">取 消</el-button>
        <el-button type="primary" @click="confirmRefund()">确 定</el-button>
      </div>
    </el-dialog>

    <pagination
      :total="page.totalRow"
      :page.sync="page.currentPage"
      :limit.sync="page.pageSize"
      @pagination="onSearch"
    />

  </div>
</template>

<script>
import elHeightAdaptiveTable from '@/directive/el-table'
import { getConfigType } from '@/api/transactRecords/index'
import { refundList, refundMang } from '@/api/refundManagement/index'

import Pagination from '@/components/Pagination' // 分页
export default {
  name: 'Refunds',
  components: {
    Pagination
  },
  directives: { elHeightAdaptiveTable },

  data() {
    return {
      quaflat: 0, // 按钮默认
      date: 1, // 按天月年
      searchKey: {
        payfee: '',
        out_trade_no: '', // 订单号
        time: '', // 账单日期
        system_no: '', // 支付场景
        System: [],
        pay_style: '', // 支付方式
        PayStyle: [],
        patientid: '',
        patientiname: ''
      },

      searchData: [],
      // { self_order_no: 1, refund_flag: 1 }, { self_order_no: 2, refund_flag: 2 }
      des: '',
      status: 1,

      addModel: { // 退款弹窗
        flag: false,
        item: {}
      },

      page: {
        tableLoading: false,
        currentPage: 1, // 当前页
        pageSize: 10, // 默认条数
        totalRow: 0, // 总条数
        totalPage: 0 // 总页数
      },
      pickerOptions: { // 日期不能点击
        disabledDate(time) {
          return time.getTime() > Date.now()
        }
      }
    }
  },

  created() {
    // var currentTime = new Date()
    this.searchKey.out_trade_no = this.$route.query.tradeNo ? this.$route.query.tradeNo : ''
    this.searchKey.time = this.$route.query.time ? this.$route.query.time : this.utils.getDay(-1, '-')
  },
  mounted() {
    this.getConfig()
    // this.onLoad()
  },
  methods: {
    expandChange(row) {
      var that = this
      row.rowFlag = !row.rowFlag
      if (row.rowFlag) {
        that.$nextTick(() => {
          that.$refs.table.toggleRowExpansion(row, true)// 加载完数据展开行
        })
      } else {
        this.$refs.table.toggleRowExpansion(row, false)// 关闭行
      }
    },
    // 系统配置搜索
    getConfig() {
      var that = this
      var params = {}
      getConfigType(params).then(res => {
        that.searchKey.PayStyle = res.data.payment // 支付方式
        that.searchKey.System = res.data.scene// 场景
      })
        .catch(() => {
        })
    },
    // 查询列表
    onLoad() {
      this.page.currentPage = 1
      this.onSearch()
    },
    onSearch() {
      var params = {}
      var that = this
      if (!that.searchKey.time) {
        that.$message({
          type: 'warning',
          message: '交易时间不能为空！'
        })
        return false
      }
      params.page = that.page.currentPage // 当前页数
      params.limit = that.page.pageSize // 每页展示条数
      params.date = that.searchKey.time
      params.type = this.$route.query.type ? this.$route.query.type : ''
      // params.date_type = that.date
      // if (that.searchKey.time && that.searchKey.time.length > 0) {
      //   params.start_time = that.searchKey.time[0]
      //   params.end_time = that.searchKey.time[1]
      // } else {
      //   params.start_time = ''
      //   params.end_time = ''
      // }
      params.status = this.$route.query.status
      params.out_trade_no = that.searchKey.out_trade_no
      params.paytype = that.searchKey.pay_style
      params.paysence = that.searchKey.system_no
      params.patientid = that.searchKey.patientid
      params.patientiname = that.searchKey.patientiname
      params.payfee = that.searchKey.payfee
      that.page.tableLoading = true
      refundList(params)
        .then(res => {
          that.searchData = []
          that.page.tableLoading = false
          that.searchData = res.data
          that.page.totalPage = res.data.pages
          that.page.totalRow = parseInt(res.data.count)
        })
        .catch(() => {
          that.page.tableLoading = false
        })
    },
    // 导出
    onExport() {
      var params = {}
      var that = this
      params.page = that.page.currentPage // 当前页数
      params.limit = that.page.pageSize // 每页展示条数
      params.date = that.searchKey.time
      params.out_trade_no = that.searchKey.out_trade_no
      params.paytype = that.searchKey.pay_style
      params.paysence = that.searchKey.system_no
      params.patientid = that.searchKey.patientid
      params.patientiname = that.searchKey.patientiname
      params.payfee = that.searchKey.payfee
      var viewUrl = `/cms/refundlist?&page=${params.page}&limit=${params.limit}&paytype=${
        params.paytype
      }&out_trade_no=${
        params.out_trade_no
      }&date=${params.date}&payfee=${params.payfee}&patientiname=${
        params.patientiname
      }&paysence=${params.paysence}&patientid=${params.patientid}&patientid=${params.patientid}&export=${1}`
      window.location.href = process.env.VUE_APP_BASE_API + viewUrl
    },
    // 退款弹窗
    refundBtn(item) {
      this.des = ''
      this.addModel.flag = true
      var items = JSON.parse(JSON.stringify(item))
      this.addModel.item = items
    },
    // 确认退款
    confirmRefund() {
      this.$confirm('确定要退款吗？退款后不可恢复！', '提示', {
        confirmButtonText: '确认',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        var params = {}
        var that = this
        params.out_trade_no = that.addModel.item.self_order_no
        params.date = that.addModel.item.trans_time.substring(0, 10)
        params.refund_fee = that.addModel.item.pay_fee
        params.des = this.des
        const loading = this.$loading({
          lock: true,
          text: 'Loading',
          spinner: 'el-icon-loading',
          background: 'rgba(255, 255, 255, 0.5)'
        })
        refundMang(params).then(res => {
          loading.close()
          if (res.code === 10000) {
            this.addModel.flag = false
            that.$message({
              type: 'success',
              message: '退款成功！'
            })
            this.searchData.forEach((ele, index) => {
              if (this.searchData[index].id === this.addModel.item.id) {
                this.searchData.splice(index, 1)
              }
            })
            // this.onSearch()
          }
        })
          .catch(() => {
            this.addModel.flag = false
            loading.close()
            this.onSearch()
            // that.page.tableLoading = false
          })
      })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消操作'
          })
        })
    },
    // 验证订单号
    suborder_no() {
      const id = this.searchKey.order_no
      const reg = /^[0-9a-zA-Z]+$/
      if (!reg.test(id)) {
        this.$message({
          type: 'warning',
          message: '请输入正确的订单号'
        })
        this.searchKey.order_no = ''
        return false
      }
    }

  }
}
</script>

<style lang="scss" scoped>
.refund {
  position: relative;
  width: 100%;
  .demo-form-inline {
    .el-button--primary {
      background-color: #579ef8 !important;
      color: #fff !important;
    }
   .el-button {
      background-color: #e8f4ff;
      color: #579ef8;
      border: 1px solid rgba(87, 158, 248, 1);
    }
  }

}
.el-form-item__content .orderItem {
  display: inline-block;
  vertical-align: top;
  width: 290px;
  margin-right: 20px;
}
.el-form-item__content .orderItem span {
  font-weight: bold;
}
ul {
  list-style: none;
  margin: 0;
  padding: 0;
}
.details {
  text-align: left;
}
.tip-title-w{
  margin: 0 10px 15px 10px;
  font-weight: 600;
  display: inline-block;
}
.el-table__expand-icon {
  display: none;
}
.el-table__expanded-cell[class*=cell]{
  padding: 10px 0;
}
</style>

